package org.reliablesource.prism.gef.editor.parts;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.reliablesource.prism.gef.editor.model.PointerLocation;
import org.reliablesource.prism.gef.editor.model.ScreenAdapter;
import org.reliablesource.prism.gef.editor.model.StickyNoteAdapter;

/**
 * 
 * PrismTreeEditPartFactory.java 
 * Factory that maps model elements to edit parts.
 * 
 * Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 * 
 * Created on: Apr 12, 2006
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */
public class PrismTreeEditPartFactory implements EditPartFactory {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart,
	 *      java.lang.Object)
	 */
	public EditPart createEditPart(final EditPart context, final Object modelElement) {
		// get EditPart for model element
		EditPart part = getPartForElement(modelElement);
		// store model element in EditPart
		part.setModel(modelElement);
		return part;
	}

	/**
	 * Maps an object to an EditPart.
	 * 
	 * @throws RuntimeException
	 *             if no match was found (programming error)
	 */
	private EditPart getPartForElement(final Object modelElement) {
		if (modelElement instanceof ScreenAdapter) {
			return new ScreenTreeEditPart((ScreenAdapter) modelElement);
		}
		if (modelElement instanceof StickyNoteAdapter) {
			return new StickyNoteTreeEditPart((StickyNoteAdapter) modelElement);
		}
//		}
		if (modelElement instanceof PointerLocation) {
			return new PointerLocationTreeEditPart((PointerLocation) modelElement);
		}
//		if (modelElement instanceof Wire) {
//			return new WireEditPart();
//		}
		throw new RuntimeException("Can't create part for model element: "
				+ ((modelElement != null) ? modelElement.getClass().getName()
						: "null"));
	}

}